using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class CommentController : Controller
    {
        //
        // GET: /Comment/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();

        public ActionResult Index(List<GOP_Y> gys)
        {
            ViewData["SAN_PHAM"] = gys[0].SAN_PHAM;

            return View(gys);
        }

        //
        // GET: /Comment/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Comment/Create

        public ActionResult Create(int id)
        {
            return View();
        } 

        //
        // POST: /Comment/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(int id, FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here
                string NoiDung = form.Get("NoiDung");
                DateTime NgayGopY = DateTime.Now;
                string TieuDe = form.Get("TieuDe");
                SAN_PHAM sp = _db.SAN_PHAM.Where((c) => c.id == id).FirstOrDefault();
                NGUOI_DUNG nd = _db.NGUOI_DUNG.Where((c) => c.TenDangNhap == User.Identity.Name).FirstOrDefault();

                GOP_Y gy = new GOP_Y
                {
                    NoiDung = NoiDung,
                    NgayGopY = NgayGopY,
                    TieuDe = TieuDe,
                    SAN_PHAM = sp,
                    NGUOI_DUNG = nd
                };

                _db.AddToGOP_Y(gy);

                _db.SaveChanges();
                sp.LOAI_SAN_PHAMReference.Load();

                return RedirectToAction("Details", sp.LOAI_SAN_PHAM.TenLoaiSanPham, new { id = id });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Comment/Edit/5
 
        public ActionResult Edit(int id)
        {
            var gy = _db.GOP_Y.Where((c) => c.id == id).FirstOrDefault();

            return View(gy);
        }

        //
        // POST: /Comment/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind()]GOP_Y gopYToEdit)
        {
            try
            {
                // TODO: Add update logic here
                gopYToEdit.NgayGopY = DateTime.Now;

                var originalGoyY = _db.GOP_Y.Where((c) => c.id == gopYToEdit.id).FirstOrDefault();

                if (!ModelState.IsValid)
                    return View(originalGoyY);

                _db.ApplyPropertyChanges(originalGoyY.EntityKey.EntitySetName, gopYToEdit);
                _db.SaveChanges();

                originalGoyY.SAN_PHAMReference.Load();
                originalGoyY.SAN_PHAM.LOAI_SAN_PHAMReference.Load();

                return RedirectToAction("Details", originalGoyY.SAN_PHAM.LOAI_SAN_PHAM.TenLoaiSanPham, new { id = originalGoyY.SAN_PHAM.id });
            }
            catch
            {
                return View();
            }
        }
    }
}
